package com.facebook.android;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Score extends Activity{
	
	private Facebook facebook;
	private String myID;
	private String app_id;
	private String score;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Getting the Bundle with the information from the Intent
        Bundle stuff = this.getIntent().getExtras();
        // Getting the APP_ID
        app_id = stuff.getString("api");
        myID = stuff.getString("myID");
        score = String.valueOf(stuff.getInt("score"));
        facebook = new Facebook(app_id);
        // Restoring the session by obtaining the session key
	    SessionStore.restore(facebook, this);
        
        setContentView(R.layout.score);
        
        String domain = "http://www.pennquiz.com/users.php";
	    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
	    nameValuePairs.add(new BasicNameValuePair("funct","updateScore"));
	    nameValuePairs.add(new BasicNameValuePair("score", score));
	    nameValuePairs.add(new BasicNameValuePair("user", myID));
	    RequestPoster rp = new RequestPoster(domain, nameValuePairs);
	     
	    rp.execute();
        
        // set up the buttons
        Button board = (Button) findViewById(R.id.board_b);
        Button done = (Button) findViewById(R.id.done_b);
        TextView total = (TextView) findViewById(R.id.total_txt);
        total.setText("Your total score from this game is: " + score);
        
        // closes this activity and opens scoreboard
        board.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final ProgressDialog progress = ProgressDialog.show(
						Score.this, "", "Loading scores...", true);
				
				final Intent intent = new Intent(v.getContext(), Scoreboard.class);
				
				// Retrieve the JSON String of friends from Facebook
				String myFriends = getFriendsJSONString(facebook);
				
				// I am sending the APP_ID and the friends 
			    // on the other side to connect both
			    // and the userID
	            intent.putExtra("api", app_id);
	            intent.putExtra("friends", myFriends);
	            intent.putExtra("myID", myID);
				// Put the dialog in a new thread so that it can close 
	            // properly afterwards
	            new Thread(new Runnable(){
	            	public void run(){
	            		finish();
	            		startActivity(intent);
	            		progress.dismiss();
	            	}
	            	}).start();
			}
        	
        });
        
        
        // finish the activity and go to main menu
        done.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
        	
        });
    }
    
    private static String getFriendsJSONString(Facebook facebook){
    	// Try to retrieve the friends from Facebook
		String myFriends = "You haven't allowed permissions";
	    try {
	    	 myFriends = facebook.request("me/friends");
	    } catch (MalformedURLException e) {
	    	 // TODO Auto-generated catch block
	    	 e.printStackTrace();
	    } catch (IOException e) {
	    	 // TODO Auto-generated catch block
	    	 e.printStackTrace();
	    }
	    return myFriends;
    }
}
